function GetSid {
	param($userName);

	try {
		$sd = new-object system.management.managementclass Win32_SecurityDescriptor 
		$ace = new-object system.management.managementclass Win32_ace
		$Trustee = new-object system.management.managementclass win32_trustee 

		$Account = new-object system.security.principal.NtAccount($userName) 
		 return $Account.translate([system.security.principal.securityidentifier]) 
	}
	catch { return "" }
	
}

function SqlUpdate {
	param($DataBaseName, $SqlCmd)
	
	$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
	$SqlConnection.ConnectionString = "Server = $SqlServer; Database = $DataBaseName; Integrated Security = True"

	#$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
	#$SqlCmd.CommandText = $SqlQuery
	$SqlCmd.Connection = $SqlConnection
	$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
	$SqlAdapter.SelectCommand = $SqlCmd
	$DataSet = New-Object System.Data.DataSet

	$null = $SqlAdapter.Fill($DataSet)
	$SqlConnection.Close()
	 
}

function GetNextVal {
	param($SqlCatalog, $SqlQuery)

	$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
	$SqlConnection.ConnectionString = "Server = $SqlServer; Database = $DataBaseName; Integrated Security = True"

	$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
	$SqlCmd.CommandText = $SqlQuery
	$SqlCmd.Connection = $SqlConnection
	$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
	$SqlAdapter.SelectCommand = $SqlCmd
	$DataSet = New-Object System.Data.DataSet

	$SqlAdapter.Fill($DataSet)
	$SqlConnection.Close()
	#Clear
	#output the data

    $dataResult = $DataSet.Tables[0]

    ForEach ($row in $DataSet.Tables[0].Rows) 
    {
        $nextValLocal = $Row.nextVal
    }

    return $nextValLocal
}

Write-Host "STOP AOSs before to start!!"
Write-Host "STOP AOSs before to start!!"

Write-Host "Please enter the SQL Server Database that contains AX Database. Example: DBServer "
 $SqlServer = Read-Host

Write-Host "Please enter the SQL Database that contains your accounts. Example: MicrosoftDynamicsAX "
 $DataBaseName = Read-Host

Write-Host "Processing..."

#Declare the file path and sheet name
$file = "D:\AXUSers.csv"

$TableIDSysUserInfo      = 956
$TableIDUserInfo         = 65531
$TableIDSECURITYUSERROLE = 65492
$TableIDOMUserRoleOrganization = 7919

Import-Csv $file | Foreach-Object { 

    foreach ($property in $_.PSObject.Properties)
    {
        If ($property.Name -eq 'Master')
        {
            $Master = $property.Value
        }
        If ($property.Name -eq 'UserID')
        {
            $UserID = $property.Value
        }
        If ($property.Name -eq 'Name')
        {
            $Name = $property.Value
        }
        If ($property.Name -eq 'Surname')
        {
            $Surname = $property.Value
        }
        If ($property.Name -eq 'Alias')
        {
            $Alias = $property.Value
        }
        If ($property.Name -eq 'Email')
        {
            $Email = $property.Value
        }

    } 
    
    $IDFROM = ""
    $SID = GetSid($Alias)

    if ($SID -ne "") 
    {         

        ################ USERINFO ######################

        # SYSTEMSEQUENCES
        $nextVal = GetNextVal $DataBaseName "SET NOCOUNT ON; SELECT nextVal FROM SYSTEMSEQUENCES where tabid = $TableIDUserInfo"
        $nextVal = $NextVal[1]

            
        $SqlQuery = "Select * from UserInfo where networkalias = '$Master'"
        $SqlConnection = New-Object System.Data.SqlClient.SqlConnection
        $SqlConnection.ConnectionString = "Server = $SqlServer; Database = $DataBaseName; Integrated Security = True"
        $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
        $SqlCmd.CommandText = $SqlQuery
        $SqlCmd.Connection = $SqlConnection
 
        $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
        $SqlAdapter.SelectCommand = $SqlCmd
        $DataSet = New-Object System.Data.DataSet
 
        $return = $SqlAdapter.Fill($DataSet)
 
        if ($return -eq 0) 
        { 
            Write-Host "Utente Master $Master non trovato!"
        }
       
        $sqlCommand = New-Object System.Data.SqlClient.SqlCommand

        ForEach ($row in $DataSet.Tables[0].Rows) 
        {
            $IDFROM = $row.ID

            $sqlCommand.CommandText = "SET NOCOUNT ON; " +
                            "INSERT INTO UserInfo ( ID, NAME, ENABLE, DEL_STARTUPMENU, STATUSLINEINFO, TOOLBARINFO, DEBUGINFO, AUTOINFO, AUTOUPDATE, GARBAGECOLLECTLIMIT, HISTORYLIMIT, MESSAGELIMIT, GENERALINFO," +
                            "SHOWSTATUSLINE, SHOWTOOLBAR, DEBUGGERPOPUP, SHOWAOTLAYER, DEL_PASSWORD, DEL_OSACCOUNTNAME, STARTUPPROJECT, CONFIRMDELETE, CONFIRMUPDATE, REPORTFONTNAME," + 
                            "REPORTFONTSIZE, FORMFONTNAME, FORMFONTSIZE, PROPERTYFONTNAME, PROPERTYFONTSIZE, INFOLOGLEVEL, COMPANY, AUTOLOGOFF, QUERYTIMELIMIT, TRACEINFO, REPORTTOPMARGIN," +
                            "REPORTBOTTOMMARGIN, REPORTLEFTMARGIN, REPORTRIGHTMARGIN, COMPILERWARNINGLEVEL, SID, NETWORKDOMAIN, NETWORKALIAS, ENABLEDONCE, EXTERNALUSER, LANGUAGE," + 
                            "HELPLANGUAGE, PREFERREDTIMEZONE, PREFERREDCALENDAR, HOMEPAGEREFRESHDURATION, NOTIFYTIMEZONEMISMATCH, FILTERBYGRIDONBYDEFAULT, GLOBALFORMOPENMODE," +  
                            "DEL_DEFAULTMODELID, SHOWMODELNAMEINAOT, ACCOUNTTYPE, ISSUERRECID, CREDENTIALRECID, GLOBALLISTPAGELINKMODE, GLOBALEXCELEXPORTMODE, GLOBALEXCELEXPORTLOCATION," + 
                            "CLIENTACCESSLOGLEVEL, DEFAULTPARTITION, GLOBALEXCELEXPORTFILEPATH, PARTITION, RECID)" +
                       "VALUES ( @ID, @NAME, @ENABLE, @DEL_STARTUPMENU, @STATUSLINEINFO, @TOOLBARINFO, @DEBUGINFO, @AUTOINFO, @AUTOUPDATE, @GARBAGECOLLECTLIMIT, @HISTORYLIMIT, @MESSAGELIMIT, @GENERALINFO," +
                            "@SHOWSTATUSLINE, @SHOWTOOLBAR, @DEBUGGERPOPUP, @SHOWAOTLAYER, @DEL_PASSWORD, @DEL_OSACCOUNTNAME, @STARTUPPROJECT, @CONFIRMDELETE, @CONFIRMUPDATE, @REPORTFONTNAME," + 
                            "@REPORTFONTSIZE, @FORMFONTNAME, @FORMFONTSIZE, @PROPERTYFONTNAME, @PROPERTYFONTSIZE, @INFOLOGLEVEL, @COMPANY, @AUTOLOGOFF, @QUERYTIMELIMIT, @TRACEINFO, @REPORTTOPMARGIN," +
                            "@REPORTBOTTOMMARGIN, @REPORTLEFTMARGIN, @REPORTRIGHTMARGIN, @COMPILERWARNINGLEVEL, @SID, @NETWORKDOMAIN, @NETWORKALIAS, @ENABLEDONCE, @EXTERNALUSER, @LANGUAGE," + 
                            "@HELPLANGUAGE, @PREFERREDTIMEZONE, @PREFERREDCALENDAR, @HOMEPAGEREFRESHDURATION, @NOTIFYTIMEZONEMISMATCH, @FILTERBYGRIDONBYDEFAULT, @GLOBALFORMOPENMODE," +  
                            "@DEL_DEFAULTMODELID, @SHOWMODELNAMEINAOT, @ACCOUNTTYPE, @ISSUERRECID, @CREDENTIALRECID, @GLOBALLISTPAGELINKMODE, @GLOBALEXCELEXPORTMODE, @GLOBALEXCELEXPORTLOCATION," + 
                            "@CLIENTACCESSLOGLEVEL, @DEFAULTPARTITION, @GLOBALEXCELEXPORTFILEPATH, @PARTITION, @RECID)"

            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@ID",[Data.SQLDBType]::NVarChar, 8))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@NAME",[Data.SQLDBType]::NVarChar, 40))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@ENABLE",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@DEL_STARTUPMENU",[Data.SQLDBType]::NVarChar, 40))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@STATUSLINEINFO",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@TOOLBARINFO",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@DEBUGINFO",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@AUTOINFO",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@AUTOUPDATE",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@GARBAGECOLLECTLIMIT",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@HISTORYLIMIT",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@MESSAGELIMIT",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@GENERALINFO",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@SHOWSTATUSLINE",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@SHOWTOOLBAR",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@DEBUGGERPOPUP",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@SHOWAOTLAYER",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@DEL_PASSWORD",[Data.SQLDBType]::NVarChar, 20))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@DEL_OSACCOUNTNAME",[Data.SQLDBType]::NVarChar, 41))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@STARTUPPROJECT",[Data.SQLDBType]::NVarChar, 40))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@CONFIRMDELETE",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@CONFIRMUPDATE",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@REPORTFONTNAME",[Data.SQLDBType]::NVarChar, 31))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@REPORTFONTSIZE",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@FORMFONTNAME",[Data.SQLDBType]::NVarChar, 31))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@FORMFONTSIZE",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@PROPERTYFONTNAME",[Data.SQLDBType]::NVarChar, 31))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@PROPERTYFONTSIZE",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@INFOLOGLEVEL",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@COMPANY",[Data.SQLDBType]::NVarChar, 4))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@AUTOLOGOFF",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@QUERYTIMELIMIT",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@TRACEINFO",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@REPORTTOPMARGIN",[Data.SQLDBType]::NVarChar, 11))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@REPORTBOTTOMMARGIN",[Data.SQLDBType]::NVarChar, 11))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@REPORTLEFTMARGIN",[Data.SQLDBType]::NVarChar, 11))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@REPORTRIGHTMARGIN",[Data.SQLDBType]::NVarChar, 11))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@COMPILERWARNINGLEVEL",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@SID",[Data.SQLDBType]::NVarChar, 124))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@NETWORKDOMAIN",[Data.SQLDBType]::NVarChar, 255))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@NETWORKALIAS",[Data.SQLDBType]::NVarChar, 80))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@ENABLEDONCE",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@EXTERNALUSER",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@LANGUAGE",[Data.SQLDBType]::NVarChar, 7))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@HELPLANGUAGE",[Data.SQLDBType]::NVarChar, 7))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@PREFERREDTIMEZONE",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@PREFERREDCALENDAR",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@HOMEPAGEREFRESHDURATION",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@NOTIFYTIMEZONEMISMATCH",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@FILTERBYGRIDONBYDEFAULT",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@GLOBALFORMOPENMODE",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@DEL_DEFAULTMODELID",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@SHOWMODELNAMEINAOT",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@ACCOUNTTYPE",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@ISSUERRECID",[Data.SQLDBType]::BigInt))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@CREDENTIALRECID",[Data.SQLDBType]::BigInt))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@GLOBALLISTPAGELINKMODE",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@GLOBALEXCELEXPORTMODE",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@GLOBALEXCELEXPORTLOCATION",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@CLIENTACCESSLOGLEVEL",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@DEFAULTPARTITION",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@GLOBALEXCELEXPORTFILEPATH",[Data.SQLDBType]::NVarChar, 259))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@PARTITION",[Data.SQLDBType]::BigInt))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@RECID",[Data.SQLDBType]::BigInt))) | Out-Null

            $sqlCommand.Parameters["@ID"].Value = $USERID
            $sqlCommand.Parameters["@NAME"].Value = $Alias
            $sqlCommand.Parameters["@ENABLE"].Value = $Row.ENABLE
            $sqlCommand.Parameters["@DEL_STARTUPMENU"].Value = $Row.DEL_STARTUPMENU
            $sqlCommand.Parameters["@STATUSLINEINFO"].Value = $Row.STATUSLINEINFO
            $sqlCommand.Parameters["@TOOLBARINFO"].Value = $Row.TOOLBARINFO
            $sqlCommand.Parameters["@DEBUGINFO"].Value = $Row.DEBUGINFO
            $sqlCommand.Parameters["@AUTOINFO"].Value = $Row.AUTOINFO
            $sqlCommand.Parameters["@AUTOUPDATE"].Value = $Row.AUTOUPDATE
            $sqlCommand.Parameters["@GARBAGECOLLECTLIMIT"].Value = $Row.GARBAGECOLLECTLIMIT
            $sqlCommand.Parameters["@HISTORYLIMIT"].Value = $Row.HISTORYLIMIT
            $sqlCommand.Parameters["@MESSAGELIMIT"].Value = $Row.MESSAGELIMIT
            $sqlCommand.Parameters["@GENERALINFO"].Value = $Row.GENERALINFO
            $sqlCommand.Parameters["@SHOWSTATUSLINE"].Value = $Row.SHOWSTATUSLINE
            $sqlCommand.Parameters["@SHOWTOOLBAR"].Value = $Row.SHOWTOOLBAR
            $sqlCommand.Parameters["@DEBUGGERPOPUP"].Value = $Row.DEBUGGERPOPUP
            $sqlCommand.Parameters["@SHOWAOTLAYER"].Value = $Row.SHOWAOTLAYER
            $sqlCommand.Parameters["@DEL_PASSWORD"].Value = $Row.DEL_PASSWORD
            $sqlCommand.Parameters["@DEL_OSACCOUNTNAME"].Value = $Row.DEL_OSACCOUNTNAME
            $sqlCommand.Parameters["@STARTUPPROJECT"].Value = $Row.STARTUPPROJECT
            $sqlCommand.Parameters["@CONFIRMDELETE"].Value = $Row.CONFIRMDELETE
            $sqlCommand.Parameters["@CONFIRMUPDATE"].Value = $Row.CONFIRMUPDATE
            $sqlCommand.Parameters["@REPORTFONTNAME"].Value = $Row.REPORTFONTNAME
            $sqlCommand.Parameters["@REPORTFONTSIZE"].Value = $Row.REPORTFONTSIZE
            $sqlCommand.Parameters["@FORMFONTNAME"].Value = $Row.FORMFONTNAME
            $sqlCommand.Parameters["@FORMFONTSIZE"].Value = $Row.FORMFONTSIZE
            $sqlCommand.Parameters["@PROPERTYFONTNAME"].Value = $Row.PROPERTYFONTNAME
            $sqlCommand.Parameters["@PROPERTYFONTSIZE"].Value = $Row.PROPERTYFONTSIZE
            $sqlCommand.Parameters["@INFOLOGLEVEL"].Value = $Row.INFOLOGLEVEL
            $sqlCommand.Parameters["@COMPANY"].Value = $Row.COMPANY
            $sqlCommand.Parameters["@AUTOLOGOFF"].Value = $Row.AUTOLOGOFF
            $sqlCommand.Parameters["@QUERYTIMELIMIT"].Value = $Row.QUERYTIMELIMIT
            $sqlCommand.Parameters["@TRACEINFO"].Value = $Row.TRACEINFO
            $sqlCommand.Parameters["@REPORTTOPMARGIN"].Value = $Row.REPORTTOPMARGIN
            $sqlCommand.Parameters["@REPORTBOTTOMMARGIN"].Value = $Row.REPORTBOTTOMMARGIN
            $sqlCommand.Parameters["@REPORTLEFTMARGIN"].Value = $Row.REPORTLEFTMARGIN
            $sqlCommand.Parameters["@REPORTRIGHTMARGIN"].Value = $Row.REPORTRIGHTMARGIN
            $sqlCommand.Parameters["@COMPILERWARNINGLEVEL"].Value = $Row.COMPILERWARNINGLEVEL
            $sqlCommand.Parameters["@SID"].Value = $SID.ToString()
            $sqlCommand.Parameters["@NETWORKDOMAIN"].Value = $Row.NETWORKDOMAIN
            $sqlCommand.Parameters["@NETWORKALIAS"].Value = $Alias
            $sqlCommand.Parameters["@ENABLEDONCE"].Value = $Row.ENABLEDONCE
            $sqlCommand.Parameters["@EXTERNALUSER"].Value = $Row.EXTERNALUSER
            $sqlCommand.Parameters["@LANGUAGE"].Value = $Row.LANGUAGE
            $sqlCommand.Parameters["@HELPLANGUAGE"].Value = $Row.HELPLANGUAGE
            $sqlCommand.Parameters["@PREFERREDTIMEZONE"].Value = $Row.PREFERREDTIMEZONE
            $sqlCommand.Parameters["@PREFERREDCALENDAR"].Value = $Row.PREFERREDCALENDAR
            $sqlCommand.Parameters["@HOMEPAGEREFRESHDURATION"].Value = $Row.HOMEPAGEREFRESHDURATION
            $sqlCommand.Parameters["@NOTIFYTIMEZONEMISMATCH"].Value = $Row.NOTIFYTIMEZONEMISMATCH
            $sqlCommand.Parameters["@FILTERBYGRIDONBYDEFAULT"].Value = $Row.FILTERBYGRIDONBYDEFAULT
            $sqlCommand.Parameters["@GLOBALFORMOPENMODE"].Value = $Row.GLOBALFORMOPENMODE
            $sqlCommand.Parameters["@DEL_DEFAULTMODELID"].Value = $Row.DEL_DEFAULTMODELID
            $sqlCommand.Parameters["@SHOWMODELNAMEINAOT"].Value = $Row.SHOWMODELNAMEINAOT
            $sqlCommand.Parameters["@ACCOUNTTYPE"].Value = $Row.ACCOUNTTYPE
            $sqlCommand.Parameters["@ISSUERRECID"].Value = $Row.ISSUERRECID
            $sqlCommand.Parameters["@CREDENTIALRECID"].Value = $Row.CREDENTIALRECID
            $sqlCommand.Parameters["@GLOBALLISTPAGELINKMODE"].Value = $Row.GLOBALLISTPAGELINKMODE
            $sqlCommand.Parameters["@GLOBALEXCELEXPORTMODE"].Value = $Row.GLOBALEXCELEXPORTMODE
            $sqlCommand.Parameters["@GLOBALEXCELEXPORTLOCATION"].Value = $Row.GLOBALEXCELEXPORTLOCATION
            $sqlCommand.Parameters["@CLIENTACCESSLOGLEVEL"].Value = $Row.CLIENTACCESSLOGLEVEL
            $sqlCommand.Parameters["@DEFAULTPARTITION"].Value = $Row.DEFAULTPARTITION
            $sqlCommand.Parameters["@GLOBALEXCELEXPORTFILEPATH"].Value = $Row.GLOBALEXCELEXPORTFILEPATH
            $sqlCommand.Parameters["@PARTITION"].Value = $Row.PARTITION

            $nextVal++
            $sqlCommand.Parameters["@RECID"].Value = $nextVal

            SqlUpdate $DataBaseName $sqlCommand;

        }

        # SYSTEMSEQUENCES
        If ( $IDFROM -ne "" )
        {
            $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
            $SqlCmd.CommandText = "UPDATE " + $DataBaseName + ".dbo.SYSTEMSEQUENCES SET nextVal =" + $nextVal + " WHERE tabId =" + $TableIDUserInfo
	        SqlUpdate $DataBaseName $SqlCmd;
        }

        ################ SYSUSERINFO ######################

        # SYSTEMSEQUENCES 
        $nextVal = GetNextVal $DataBaseName "SET NOCOUNT ON; SELECT nextVal FROM SYSTEMSEQUENCES where tabid = $TableIDSysUserInfo"
        $nextVal = $NextVal[1]
            
        $SqlQuery = "Select * from SysUserInfo where ID = '" + $IDFROM + "'"
        $SqlConnection = New-Object System.Data.SqlClient.SqlConnection
        $SqlConnection.ConnectionString = "Server = $SqlServer; Database = $DataBaseName; Integrated Security = True"
        $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
        $SqlCmd.CommandText = $SqlQuery
        $SqlCmd.Connection = $SqlConnection
 
        $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
        $SqlAdapter.SelectCommand = $SqlCmd
        $DataSet = New-Object System.Data.DataSet
 
        $return = $SqlAdapter.Fill($DataSet)
 
        $sqlCommand = New-Object System.Data.SqlClient.SqlCommand
        ForEach ($row in $DataSet.Tables[0].Rows) 
        {
            $sqlCommand.CommandText = "SET NOCOUNT ON; " +
                        "INSERT INTO SysUserInfo ( ID, DOCUHANDLINGACTIVE, DOCUTOOLBARBUTTONACTIVE, HELPMARKEMPTYLINKS, HELPTHEME, EMAIL, COMPILERTARGET, LANGUAGE,  
                         DEFAULTCOUNTRYREGION, EVENTPOLLFREQUENCY, EVENTPOPUPLINKDESTINATION, EVENTPOPUPDISPLAYWHEN, EVENTEMAILALERTSWHEN, EVENTPOPUPS, EVENTWORKFLOWTASKSINEMAIL, 
                         WORKFLOWLINEITEMNOTIFICATIONFORMAT, EVENTWORKFLOWTASKSINCLIENT, EVENTWORKFLOWSHOWPOPUP, PARTITION, RECID)" +
                       "VALUES ( @ID, @DOCUHANDLINGACTIVE, @DOCUTOOLBARBUTTONACTIVE, @HELPMARKEMPTYLINKS, @HELPTHEME, @EMAIL, @COMPILERTARGET, @LANGUAGE, 
                         @DEFAULTCOUNTRYREGION, @EVENTPOLLFREQUENCY, @EVENTPOPUPLINKDESTINATION, @EVENTPOPUPDISPLAYWHEN, @EVENTEMAILALERTSWHEN, @EVENTPOPUPS, @EVENTWORKFLOWTASKSINEMAIL, 
                         @WORKFLOWLINEITEMNOTIFICATIONFORMAT, @EVENTWORKFLOWTASKSINCLIENT, @EVENTWORKFLOWSHOWPOPUP, @PARTITION, @RECID)"

            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@ID",[Data.SQLDBType]::NVarChar, 8))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@DOCUHANDLINGACTIVE",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@DOCUTOOLBARBUTTONACTIVE",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@HELPMARKEMPTYLINKS",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@HELPTHEME",[Data.SQLDBType]::NVarChar, 60))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@EMAIL",[Data.SQLDBType]::NVarChar, 80))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@COMPILERTARGET",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@LANGUAGE",[Data.SQLDBType]::NVarChar, 7))) | Out-Null           
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@DEFAULTCOUNTRYREGION",[Data.SQLDBType]::NVarChar, 20))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@EVENTPOLLFREQUENCY",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@EVENTPOPUPLINKDESTINATION",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@EVENTPOPUPDISPLAYWHEN",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@EVENTEMAILALERTSWHEN",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@EVENTPOPUPS",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@EVENTWORKFLOWTASKSINEMAIL",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@WORKFLOWLINEITEMNOTIFICATIONFORMAT",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@EVENTWORKFLOWTASKSINCLIENT",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@EVENTWORKFLOWSHOWPOPUP",[Data.SQLDBType]::Int))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@PARTITION",[Data.SQLDBType]::BigInt))) | Out-Null
            $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@RECID",[Data.SQLDBType]::BigInt))) | Out-Null

            $sqlCommand.Parameters["@ID"].Value = $USERID
            $sqlCommand.Parameters["@DOCUHANDLINGACTIVE"].Value = $Row.DOCUHANDLINGACTIVE
            $sqlCommand.Parameters["@DOCUTOOLBARBUTTONACTIVE"].Value = $Row.DOCUTOOLBARBUTTONACTIVE
            $sqlCommand.Parameters["@HELPMARKEMPTYLINKS"].Value = $Row.HELPMARKEMPTYLINKS
            $sqlCommand.Parameters["@HELPTHEME"].Value = $Row.HELPTHEME
            $sqlCommand.Parameters["@EMAIL"].Value = $EMAIL
            $sqlCommand.Parameters["@COMPILERTARGET"].Value = $Row.COMPILERTARGET
            $sqlCommand.Parameters["@LANGUAGE"].Value = $Row.LANGUAGE
           
            $sqlCommand.Parameters["@DEFAULTCOUNTRYREGION"].Value = $Row.DEFAULTCOUNTRYREGION
            $sqlCommand.Parameters["@EVENTPOLLFREQUENCY"].Value = $Row.EVENTPOLLFREQUENCY
            $sqlCommand.Parameters["@EVENTPOPUPLINKDESTINATION"].Value = $Row.EVENTPOPUPLINKDESTINATION
            $sqlCommand.Parameters["@EVENTPOPUPDISPLAYWHEN"].Value = $Row.EVENTPOPUPDISPLAYWHEN
            $sqlCommand.Parameters["@EVENTEMAILALERTSWHEN"].Value = $Row.EVENTEMAILALERTSWHEN
            $sqlCommand.Parameters["@EVENTPOPUPS"].Value = $Row.EVENTPOPUPS
            $sqlCommand.Parameters["@EVENTWORKFLOWTASKSINEMAIL"].Value = $Row.EVENTWORKFLOWTASKSINEMAIL
            $sqlCommand.Parameters["@WORKFLOWLINEITEMNOTIFICATIONFORMAT"].Value = $Row.WORKFLOWLINEITEMNOTIFICATIONFORMAT
            $sqlCommand.Parameters["@EVENTWORKFLOWTASKSINCLIENT"].Value = $Row.EVENTWORKFLOWTASKSINCLIENT
            $sqlCommand.Parameters["@EVENTWORKFLOWSHOWPOPUP"].Value = $Row.EVENTWORKFLOWSHOWPOPUP
            $sqlCommand.Parameters["@PARTITION"].Value = $Row.PARTITION

            $nextVal++
            $sqlCommand.Parameters["@RECID"].Value = $nextVal

            SqlUpdate $DataBaseName $sqlCommand;
        }

        # SYSTEMSEQUENCES
        If ( $IDFROM -ne "" )
        {
            $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
            $SqlCmd.CommandText = "UPDATE " + $DataBaseName + ".dbo.SYSTEMSEQUENCES SET nextVal =" + $nextVal + " WHERE tabId =" + $TableIDSysUserInfo
	        SqlUpdate $DataBaseName $SqlCmd;
        }

        ################ SECURITYUSERROLE ######################

        # SYSTEMSEQUENCES 
        $nextVal = GetNextVal $DataBaseName "SET NOCOUNT ON; SELECT nextVal FROM SYSTEMSEQUENCES where tabid = $TableIDSECURITYUSERROLE"
        $nextVal = $NextVal[1]

            
        $SqlQuery = "Select * from SECURITYUSERROLE where USER_ = '" + $IDFROM + "'"
        $SqlConnection = New-Object System.Data.SqlClient.SqlConnection
        $SqlConnection.ConnectionString = "Server = $SqlServer; Database = $DataBaseName; Integrated Security = True"
        $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
        $SqlCmd.CommandText = $SqlQuery
        $SqlCmd.Connection = $SqlConnection
 
        $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
        $SqlAdapter.SelectCommand = $SqlCmd
        $DataSet = New-Object System.Data.DataSet
 
        $return = $SqlAdapter.Fill($DataSet)
 
        $sqlCommand = New-Object System.Data.SqlClient.SqlCommand
        $sqlCommand.CommandText = "SET NOCOUNT ON; " +
                    "INSERT INTO SECURITYUSERROLE ( USER_, SECURITYROLE, ASSIGNMENTSTATUS, ASSIGNMENTMODE, VALIDFROM, VALIDFROMTZID, VALIDTO, VALIDTOTZID, 
                        PARTITION, RECID)" +
                    "VALUES ( @USER_, @SECURITYROLE, @ASSIGNMENTSTATUS, @ASSIGNMENTMODE, @VALIDFROM, @VALIDFROMTZID, @VALIDTO, @VALIDTOTZID, 
                                @PARTITION, @RECID)"

        $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@USER_",[Data.SQLDBType]::NVarChar, 8))) | Out-Null
        $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@SECURITYROLE",[Data.SQLDBType]::bigInt))) | Out-Null
        $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@ASSIGNMENTSTATUS",[Data.SQLDBType]::Int))) | Out-Null
        $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@ASSIGNMENTMODE",[Data.SQLDBType]::Int))) | Out-Null
        $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@VALIDFROM",[Data.SQLDBType]::DateTime))) | Out-Null
        $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@VALIDFROMTZID",[Data.SQLDBType]::Int))) | Out-Null
        $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@VALIDTO",[Data.SQLDBType]::DateTime))) | Out-Null
        $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@VALIDTOTZID",[Data.SQLDBType]::Int))) | Out-Null
            
        $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@PARTITION",[Data.SQLDBType]::BigInt))) | Out-Null
        $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@RECID",[Data.SQLDBType]::BigInt))) | Out-Null

        ForEach ($row in $DataSet.Tables[0].Rows) 
        {            
            $sqlCommand.Parameters["@USER_"].Value = $USERID
            $sqlCommand.Parameters["@SECURITYROLE"].Value = $Row.SECURITYROLE
            $sqlCommand.Parameters["@ASSIGNMENTSTATUS"].Value = $Row.ASSIGNMENTSTATUS
            $sqlCommand.Parameters["@ASSIGNMENTMODE"].Value = $Row.ASSIGNMENTMODE
            $sqlCommand.Parameters["@VALIDFROM"].Value = $Row.VALIDFROM
            $sqlCommand.Parameters["@VALIDFROMTZID"].Value = $Row.VALIDFROMTZID
            $sqlCommand.Parameters["@VALIDTO"].Value = $Row.VALIDTO
            $sqlCommand.Parameters["@VALIDTOTZID"].Value = $Row.VALIDTOTZID

            $sqlCommand.Parameters["@PARTITION"].Value = $Row.PARTITION

            $NextVal++
            $sqlCommand.Parameters["@RECID"].Value = $NextVal

            SqlUpdate $DataBaseName $sqlCommand;
        }

        # SYSTEMSEQUENCES
        If ( $IDFROM -ne "" )
        {
            $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
            $SqlCmd.CommandText = "UPDATE " + $DataBaseName + ".dbo.SYSTEMSEQUENCES SET nextVal =" + $nextVal + " WHERE tabId =" + $TableIDSECURITYUSERROLE
	        SqlUpdate $DataBaseName $SqlCmd;
        }

        ################ OMUserRoleOrganization ######################

        # SYSTEMSEQUENCES 
        $nextVal = GetNextVal $DataBaseName "SET NOCOUNT ON; SELECT nextVal FROM SYSTEMSEQUENCES where tabid = $TableIDOMUserRoleOrganization"
        $nextVal = $NextVal[1]

            
        $SqlQuery = "Select * from OMUserRoleOrganization where USER_ = '" + $IDFROM + "'"
        $SqlConnection = New-Object System.Data.SqlClient.SqlConnection
        $SqlConnection.ConnectionString = "Server = $SqlServer; Database = $DataBaseName; Integrated Security = True"
        $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
        $SqlCmd.CommandText = $SqlQuery
        $SqlCmd.Connection = $SqlConnection
 
        $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
        $SqlAdapter.SelectCommand = $SqlCmd
        $DataSet = New-Object System.Data.DataSet
 
        $return = $SqlAdapter.Fill($DataSet)
 
        $sqlCommand = New-Object System.Data.SqlClient.SqlCommand
        $sqlCommand.CommandText = "SET NOCOUNT ON; " +
                    "INSERT INTO OMUserRoleOrganization ( USER_, OMINTERNALORGANIZATION, OMHIERARCHYTYPE, SECURITYROLEASSIGNMENTRULE, SECURITYROLE,
                        PARTITION, RECID)" +
                    "VALUES ( @USER_, @OMINTERNALORGANIZATION, @OMHIERARCHYTYPE, @SECURITYROLEASSIGNMENTRULE, @SECURITYROLE, 
                                @PARTITION, @RECID)"

        $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@USER_",[Data.SQLDBType]::NVarChar, 8))) | Out-Null
        $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@OMINTERNALORGANIZATION",[Data.SQLDBType]::bigInt))) | Out-Null
        $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@OMHIERARCHYTYPE",[Data.SQLDBType]::Int))) | Out-Null
        $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@SECURITYROLEASSIGNMENTRULE",[Data.SQLDBType]::BigInt))) | Out-Null
        $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@SECURITYROLE",[Data.SQLDBType]::BigInt))) | Out-Null
            
        $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@PARTITION",[Data.SQLDBType]::BigInt))) | Out-Null
        $sqlCommand.Parameters.Add((New-Object Data.SqlClient.SqlParameter("@RECID",[Data.SQLDBType]::BigInt))) | Out-Null

        ForEach ($row in $DataSet.Tables[0].Rows) 
        {            
            $sqlCommand.Parameters["@USER_"].Value = $USERID
            $sqlCommand.Parameters["@OMINTERNALORGANIZATION"].Value = $Row.OMINTERNALORGANIZATION
            $sqlCommand.Parameters["@OMHIERARCHYTYPE"].Value = $Row.OMHIERARCHYTYPE
            $sqlCommand.Parameters["@SECURITYROLEASSIGNMENTRULE"].Value = $Row.SECURITYROLEASSIGNMENTRULE
            $sqlCommand.Parameters["@SECURITYROLE"].Value = $Row.SECURITYROLE

            $sqlCommand.Parameters["@PARTITION"].Value = $Row.PARTITION

            $NextVal++
            $sqlCommand.Parameters["@RECID"].Value = $NextVal

            SqlUpdate $DataBaseName $sqlCommand;
        }

        # SYSTEMSEQUENCES
        If ( $IDFROM -ne "" )
        {
            $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
            $SqlCmd.CommandText = "UPDATE " + $DataBaseName + ".dbo.SYSTEMSEQUENCES SET nextVal =" + $nextVal + " WHERE tabId =" + $TableIDOMUserRoleOrganization
	        SqlUpdate $DataBaseName $SqlCmd;
        }
        ####################
        
        If ( $IDFROM -ne "" )
        {
            Write-Host "Utente " $Alias " Aggiunto con Successo!"
        }
    }
    Else
    {
        Write-Host "Utente " $Alias " Non Trovato !"
    }

}


$SqlConnection.Close()
exit